package fr.norsys.mapper.console.web;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.navigator.menu.MenuRepository;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import fr.norsys.mapper.console.model.Application;
import fr.norsys.mapper.console.model.ConsoleCst;
import fr.norsys.mapper.console.model.Variable;
import fr.norsys.mapper.console.service.ApplicationService;
import fr.norsys.mapper.console.service.MenuService;
import fr.norsys.mapper.console.service.VariableService;

public class VariableAction extends BaseAppDispatchAction {
	private static Log log = LogFactory.getLog(VariableAction.class);

	private VariableService variableService;

	private ApplicationService applicationService;

	private MenuService menuService;

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	/**
	 * Show the variables list of the current application
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("list method called");
		}
		DynaActionForm variableForm = (DynaActionForm) form;
		Application currentAppl = (Application) request.getSession()
				.getAttribute(ConsoleCst.CURRENT_APPLICATION);
		variableForm.set(ConsoleCst.APPLICATION_REQUEST_BEAN, currentAppl);
		refreshMenu(request);
		return mapping.findForward("index");
	}

	/**
	 * This method refreshs the menu.
	 */
	private void refreshMenu(HttpServletRequest request) throws Exception {
		MenuRepository oldRepository = (MenuRepository) getServlet()
				.getServletContext().getAttribute(
						MenuRepository.MENU_REPOSITORY_KEY);
		MenuRepository menu = menuService.buildMenu(oldRepository,
				(Collection) request.getSession().getAttribute(
						ConsoleCst.APPLICATIONS_SESSION_BEAN),
				(Collection) request.getSession().getAttribute(
						ConsoleCst.CONNECTIONS_SESSION_BEAN), (String) request
						.getSession().getAttribute(
								ConsoleCst.IS_APPLICATION_MODIFIED),
				(String) request.getSession().getAttribute(
						ConsoleCst.APPLICATION_ID));
		request.getSession().setAttribute(ConsoleCst.NODE_LIST, menu);
	}

	/**
	 * Delete a variable
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("delete method called");
		}
		Application currentAppl = (Application) request.getSession()
				.getAttribute(ConsoleCst.CURRENT_APPLICATION);
		String id = request.getParameter("varId");
		variableService.delete(id, currentAppl.getVariables());
		request.getSession().setAttribute(ConsoleCst.IS_APPLICATION_MODIFIED,
				ConsoleCst.APPLICATION_MODIFIED);
		return list(mapping, form, request, response);
	}

	/**
	 * Edit a variable
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("edit method called");
		}
		String id = request.getParameter("id");
		DynaActionForm variableForm = (DynaActionForm) form;
		if (id != null && !"".equals(id)) {
			Application currentAppl = (Application) request.getSession()
					.getAttribute(ConsoleCst.CURRENT_APPLICATION);
			Variable variable = variableService.get(id, currentAppl
					.getVariables());
			variableForm.set(ConsoleCst.VARIABLE_REQUEST_BEAN, variable);
		}
		request.getSession().setAttribute(ConsoleCst.IS_APPLICATION_MODIFIED,
				ConsoleCst.APPLICATION_MODIFIED);
		return mapping.findForward("popup");
	}

	public VariableService getVariableService() {
		return variableService;
	}

	public void setVariableService(VariableService variableService) {
		this.variableService = variableService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}
}